
 <!DOCTYPE HTML>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  
    <title>RxJava基础篇(三):常用操作符 | RyanHuen</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">
    
    <meta name="author" content="RyanHuen">
    

    
    <meta name="description" content="变换：
RX提供了对事件序列进行变换的支持，这是核心功能之一。就是将事件序列中的对象进行加工处理，转换成不同的事件或者事件序列（简单点说：发送出来的事件，我们可以理解成一个对象，事件序列就是对象序列。对事件进行改造加工，就是对对象进行额外处理）。


变换操作符：
Map：
对上游发送的每一个事件，应用一个操作，让每一个经过这个操作的事件，都能按照指定的方式去发生变换。

图例：


通过Map">
<meta property="og:type" content="article">
<meta property="og:title" content="RxJava基础篇(三):常用操作符">
<meta property="og:url" content="http://www.ryanhuen.tech/2017/02/17/rx-base-operator/index.html">
<meta property="og:site_name" content="RyanHuen">
<meta property="og:description" content="变换：
RX提供了对事件序列进行变换的支持，这是核心功能之一。就是将事件序列中的对象进行加工处理，转换成不同的事件或者事件序列（简单点说：发送出来的事件，我们可以理解成一个对象，事件序列就是对象序列。对事件进行改造加工，就是对对象进行额外处理）。


变换操作符：
Map：
对上游发送的每一个事件，应用一个操作，让每一个经过这个操作的事件，都能按照指定的方式去发生变换。

图例：


通过Map">
<meta property="og:image" content="http://olns0cxf5.bkt.clouddn.com/articleImages/rx/rx-operator-1.png?watermark/2/text/Unlhbkh1ZW4=/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0JCQkJCQg==/dissolve/100/gravity/SouthWest/dx/10/dy/10">
<meta property="og:image" content="http://olns0cxf5.bkt.clouddn.com/articleImages/rx/rx-operator-2.png?watermark/2/text/Unlhbkh1ZW4=/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0JCQkJCQg==/dissolve/100/gravity/SouthWest/dx/10/dy/10">
<meta property="og:image" content="http://olns0cxf5.bkt.clouddn.com/articleImages/rx/rx-operator-3.png?watermark/2/text/Unlhbkh1ZW4=/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0JCQkJCQg==/dissolve/100/gravity/SouthWest/dx/10/dy/10">
<meta property="og:image" content="http://olns0cxf5.bkt.clouddn.com/articleImages/rx/rx-operator-4.png?watermark/2/text/Unlhbkh1ZW4=/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0JCQkJCQg==/dissolve/100/gravity/SouthWest/dx/10/dy/10">
<meta property="og:updated_time" content="2017-02-22T03:05:43.110Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="RxJava基础篇(三):常用操作符">
<meta name="twitter:description" content="变换：
RX提供了对事件序列进行变换的支持，这是核心功能之一。就是将事件序列中的对象进行加工处理，转换成不同的事件或者事件序列（简单点说：发送出来的事件，我们可以理解成一个对象，事件序列就是对象序列。对事件进行改造加工，就是对对象进行额外处理）。


变换操作符：
Map：
对上游发送的每一个事件，应用一个操作，让每一个经过这个操作的事件，都能按照指定的方式去发生变换。

图例：


通过Map">
<meta name="twitter:image" content="http://olns0cxf5.bkt.clouddn.com/articleImages/rx/rx-operator-1.png?watermark/2/text/Unlhbkh1ZW4=/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0JCQkJCQg==/dissolve/100/gravity/SouthWest/dx/10/dy/10">

    
    <link rel="alternative" href="/atom.xml" title="RyanHuen" type="application/atom+xml">
    
    
    <link rel="icon" href="/img/favicon.ico">
    
    
    <link rel="apple-touch-icon" href="/img/jacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/img/jacman.jpg">
    
    <link rel="stylesheet" href="/css/style.css">
</head>

  <body>
    <header>
      
<div>
		
			<div id="imglogo">
				<a href="/"><img src="/img/logo.png" alt="RyanHuen" title="RyanHuen"/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/" title="RyanHuen">RyanHuen</a></h1>
				<h2 class="blog-motto">I hope I can protect the one thing I can&#39;t live without.</h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="菜单">
			</a></div>
			<nav class="animated">
				<ul>
					<ul>
					 
						<li><a href="/">首页</a></li>
					
						<li><a href="/archives">历程</a></li>
					
						<li><a href="/categories">分类</a></li>
					
						<li><a href="/tags">标签</a></li>
					
						<li><a href="/about">关于</a></li>
					
					<li>
 					
					<form class="search" action="//google.com/search" method="get" accept-charset="utf-8">
						<label>Search</label>
						<input type="search" id="search" name="q" autocomplete="off" maxlength="20" placeholder="搜索" />
						<input type="hidden" name="q" value="site:www.ryanhuen.tech">
					</form>
					
					</li>
				</ul>
			</nav>			
</div>
    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
  
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/2017/02/17/rx-base-operator/" title="RxJava基础篇(三):常用操作符" itemprop="url">RxJava基础篇(三):常用操作符</a>
  </h1>
  <p class="article-author">By
       
		<a href="/about" title="RyanHuen" target="_blank" itemprop="author">RyanHuen</a>
		
  <p class="article-time">
    <time datetime="2017-02-17T02:33:45.000Z" itemprop="datePublished"> 发表于 2017-02-17</time>
    
  </p>
</header>
	<div class="article-content">
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">文章目录</strong>
		
			<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#变换："><span class="toc-number">1.</span> <span class="toc-text">变换：</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#变换操作符："><span class="toc-number">2.</span> <span class="toc-text">变换操作符：</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Map："><span class="toc-number">2.1.</span> <span class="toc-text">Map：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#FlatMap："><span class="toc-number">2.2.</span> <span class="toc-text">FlatMap：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#ConcatMap："><span class="toc-number">2.3.</span> <span class="toc-text">ConcatMap：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Zip："><span class="toc-number">2.4.</span> <span class="toc-text">Zip：</span></a></li></ol></li></ol>
		
		</div>
		
		<p><a id="orga8730bd"></a></p>
<h1 id="变换："><a href="#变换：" class="headerlink" title="变换："></a>变换：</h1><ol>
<li>RX提供了对事件序列进行变换的支持，这是核心功能之一。就是将事件序列中的对象进行加工处理，转换成不同的事件或者事件序列（简单点说：发送出来的事件，我们可以理解成一个对象，事件序列就是对象序列。对事件进行改造加工，就是对对象进行额外处理）。</li>
</ol>
<p><a id="orgd425b69"></a></p>
<h1 id="变换操作符："><a href="#变换操作符：" class="headerlink" title="变换操作符："></a>变换操作符：</h1><p><a id="org5e5b315"></a></p>
<h2 id="Map："><a href="#Map：" class="headerlink" title="Map："></a>Map：</h2><ol>
<li><p>对上游发送的每一个事件，应用一个操作，让每一个经过这个操作的事件，都能按照指定的方式去发生变换。</p>
</li>
<li><p>图例：</p>
<p><img src="http://olns0cxf5.bkt.clouddn.com/articleImages/rx/rx-operator-1.png?watermark/2/text/Unlhbkh1ZW4=/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0JCQkJCQg==/dissolve/100/gravity/SouthWest/dx/10/dy/10" alt="img"></p>
</li>
<li><p>通过Map我们可以将上游发射过来的任意事件转换为我们想要的任意类型：</p>
<ol>
<li>Object、集合、自定义对象等等等等</li>
</ol>
</li>
<li><p>请看代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">Observable.create(<span class="keyword">new</span> ObservableOnSubscribe&lt;String&gt;() &#123;</div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">subscribe</span><span class="params">(ObservableEmitter&lt;String&gt; e<span class="comment">/* 事件发射器 */</span>)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">            e.onNext(<span class="string">"事件1"</span>);</div><div class="line">            e.onNext(<span class="string">"事件2"</span>);</div><div class="line">            e.onNext(<span class="string">"事件3"</span>);</div><div class="line">            e.onNext(<span class="string">"事件4"</span>);</div><div class="line">            e.onComplete();</div><div class="line">        &#125;</div><div class="line">    &#125;).map(<span class="keyword">new</span> Function&lt;String, String&gt;() &#123;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> String <span class="title">apply</span><span class="params">(String string)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">                <span class="comment">// 通过变换操作，拼接字符串，随后返回</span></div><div class="line">                <span class="keyword">return</span> <span class="string">"this is resule:"</span> + string;</div><div class="line">            &#125;</div><div class="line">        &#125;).subscribe(<span class="keyword">new</span> Consumer&lt;String&gt;() &#123;</div><div class="line">                <span class="meta">@Override</span></div><div class="line">                <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">accept</span><span class="params">(String s)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">                    Log.d(TAG, <span class="string">"accept: "</span> + s);</div><div class="line">                &#125;</div><div class="line">            &#125;);</div></pre></td></tr></table></figure>
</li>
</ol>
<p><a id="orgcb1608a"></a></p>
<h2 id="FlatMap："><a href="#FlatMap：" class="headerlink" title="FlatMap："></a>FlatMap：</h2><ol>
<li><p>将一个发送事件的上游Observable变换成多个发送事件的Observables，然后将他们发射的事件合并后，放入一个单独的Observable中，随后交给下游处理。（可以理解成，第一个上游，创建了多个上游发射事件，随后将这多个上游发射的事件汇总到一起，交给下游处理）</p>
</li>
<li><p>图例：</p>
<p><img src="http://olns0cxf5.bkt.clouddn.com/articleImages/rx/rx-operator-2.png?watermark/2/text/Unlhbkh1ZW4=/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0JCQkJCQg==/dissolve/100/gravity/SouthWest/dx/10/dy/10" alt="img"></p>
</li>
</ol>
<ol>
<li><p>注意：FlatMap并不保证事件的发送顺序</p>
</li>
<li><p>请看代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line">Observable.create(<span class="keyword">new</span> ObservableOnSubscribe&lt;String&gt;() &#123;</div><div class="line">     <span class="meta">@Override</span></div><div class="line">     <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">subscribe</span><span class="params">(ObservableEmitter&lt;String&gt; e<span class="comment">/* 事件发射器 */</span>)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">         e.onNext(<span class="string">"事件1"</span>);</div><div class="line">         e.onNext(<span class="string">"事件2"</span>);</div><div class="line">         e.onNext(<span class="string">"事件3"</span>);</div><div class="line">         e.onNext(<span class="string">"事件4"</span>);</div><div class="line">         e.onComplete();</div><div class="line">     &#125;</div><div class="line"> &#125;).flatMap(<span class="keyword">new</span> Function&lt;String, ObservableSource&lt;String&gt;&gt;() &#123;</div><div class="line">     <span class="meta">@Override</span></div><div class="line">     <span class="function"><span class="keyword">public</span> ObservableSource&lt;String&gt; <span class="title">apply</span><span class="params">(String string)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">         <span class="keyword">final</span> List&lt;String&gt; list = <span class="keyword">new</span> ArrayList&lt;String&gt;();</div><div class="line">         <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++) &#123;</div><div class="line">             list.add(<span class="string">"I am value:"</span> + string);</div><div class="line">         &#125;</div><div class="line">         <span class="keyword">return</span> Observable.fromIterable(list)<span class="comment">/*</span></div><div class="line">                                              * 后续代码加入了delay功能，</div><div class="line">                                              * 标示发送事件延迟10毫秒</div><div class="line">                                              */.delay(<span class="number">10</span>, TimeUnit.MILLISECONDS);</div><div class="line">     &#125;</div><div class="line"> &#125;).subscribe(<span class="keyword">new</span> Consumer&lt;String&gt;() &#123;</div><div class="line">     <span class="meta">@Override</span></div><div class="line">     <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">accept</span><span class="params">(String s)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">         Log.d(TAG, <span class="string">"accept: "</span> + s);</div><div class="line">     &#125;</div><div class="line"> &#125;);</div></pre></td></tr></table></figure>
</li>
</ol>
<p><a id="org1ee809f"></a></p>
<h2 id="ConcatMap："><a href="#ConcatMap：" class="headerlink" title="ConcatMap："></a>ConcatMap：</h2><ol>
<li><p>与FlatMap的效果几乎一模一样</p>
</li>
<li><p>图例：</p>
<p><img src="http://olns0cxf5.bkt.clouddn.com/articleImages/rx/rx-operator-3.png?watermark/2/text/Unlhbkh1ZW4=/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0JCQkJCQg==/dissolve/100/gravity/SouthWest/dx/10/dy/10" alt="img"></p>
</li>
</ol>
<ol>
<li><p>区别：</p>
</li>
<li><p>它的结果是严格按照上游的发送顺序来进行发送的</p>
</li>
<li><p>请看代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line">Observable.create(<span class="keyword">new</span> ObservableOnSubscribe&lt;String&gt;() &#123;</div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">subscribe</span><span class="params">(ObservableEmitter&lt;String&gt; e<span class="comment">/* 事件发射器 */</span>)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">            e.onNext(<span class="string">"事件1"</span>);</div><div class="line">            e.onNext(<span class="string">"事件2"</span>);</div><div class="line">            e.onNext(<span class="string">"事件3"</span>);</div><div class="line">            e.onNext(<span class="string">"事件4"</span>);</div><div class="line">            e.onComplete();</div><div class="line">        &#125;</div><div class="line">    &#125;).concatMap(<span class="keyword">new</span> Function&lt;String, ObservableSource&lt;String&gt;&gt;() &#123;</div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="function"><span class="keyword">public</span> ObservableSource&lt;String&gt; <span class="title">apply</span><span class="params">(String string)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">            <span class="keyword">final</span> List&lt;String&gt; list = <span class="keyword">new</span> ArrayList&lt;String&gt;();</div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++) &#123;</div><div class="line">                list.add(<span class="string">"I am value:"</span> + string);</div><div class="line">            &#125;</div><div class="line">            <span class="keyword">return</span> Observable.fromIterable(list)<span class="comment">/*</span></div><div class="line">                                                 * 后续代码加入了delay功能，</div><div class="line">                                                 * 标示发送事件延迟10毫秒</div><div class="line">                                                 */.delay(<span class="number">10</span>, TimeUnit.MILLISECONDS);</div><div class="line">        &#125;</div><div class="line">    &#125;).subscribe(<span class="keyword">new</span> Consumer&lt;String&gt;() &#123;</div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">accept</span><span class="params">(String s)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">            Log.d(TAG, <span class="string">"accept: "</span> + s);</div><div class="line">        &#125;</div><div class="line">    &#125;);</div></pre></td></tr></table></figure>
</li>
</ol>
<p><a id="org7502f58"></a></p>
<h2 id="Zip："><a href="#Zip：" class="headerlink" title="Zip："></a>Zip：</h2><ol>
<li><p>通过一个函数将两个个Observable发送的事件整合到一起，将他们发送给下游。</p>
</li>
<li><p>组合的顺序是严格按照事件发送的顺序来进行的，也就是说每个上游的事件顺序都是对应进行zip处理的。</p>
</li>
<li><p>zip整合后的事件发射个数：取两个个上游中，事件个数最少的上游的个数为准。因为zip是从每个上游取出一个事件进行处理，一旦有任何一个上游的事件取完，zip就结束了。</p>
</li>
<li><p>具体流程：</p>
<p><img src="http://olns0cxf5.bkt.clouddn.com/articleImages/rx/rx-operator-4.png?watermark/2/text/Unlhbkh1ZW4=/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0JCQkJCQg==/dissolve/100/gravity/SouthWest/dx/10/dy/10" alt="img"></p>
</li>
</ol>
<ol>
<li><p>请看代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div></pre></td><td class="code"><pre><div class="line">Observable&lt;String&gt; observable1 = Observable.create(<span class="keyword">new</span> ObservableOnSubscribe&lt;String&gt;() &#123;</div><div class="line">           <span class="meta">@Override</span></div><div class="line">           <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">subscribe</span><span class="params">(ObservableEmitter&lt;String&gt; e<span class="comment">/* 事件发射器 */</span>)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">               e.onNext(<span class="string">"01我是："</span>);</div><div class="line">               e.onNext(<span class="string">"02我是："</span>);</div><div class="line">               e.onNext(<span class="string">"03我是："</span>);</div><div class="line">               e.onNext(<span class="string">"04我是："</span>);</div><div class="line">               e.onComplete();</div><div class="line">           &#125;</div><div class="line">       &#125;).subscribeOn(Schedulers.io());</div><div class="line">       Observable&lt;Integer&gt; observable2 = Observable.create(<span class="keyword">new</span> ObservableOnSubscribe&lt;Integer&gt;() &#123;</div><div class="line">           <span class="meta">@Override</span></div><div class="line">           <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">subscribe</span><span class="params">(ObservableEmitter&lt;Integer&gt; e<span class="comment">/* 事件发射器 */</span>)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">               e.onNext(<span class="number">1</span>);</div><div class="line">               e.onNext(<span class="number">2</span>);</div><div class="line">               e.onNext(<span class="number">3</span>);</div><div class="line">               e.onComplete();</div><div class="line">           &#125;</div><div class="line">       &#125;).subscribeOn(Schedulers.io());</div><div class="line">       Observable.zip(observable1, observable2, <span class="keyword">new</span> BiFunction&lt;String, Integer, String&gt;() &#123;</div><div class="line">           <span class="meta">@Override</span></div><div class="line">           <span class="function"><span class="keyword">public</span> String <span class="title">apply</span><span class="params">(String integer, Integer integer2)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">               <span class="keyword">return</span> integer + integer2;</div><div class="line">           &#125;</div><div class="line">       &#125;).subscribe(<span class="keyword">new</span> Observer&lt;String&gt;() &#123;</div><div class="line">           <span class="meta">@Override</span></div><div class="line">           <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onSubscribe</span><span class="params">(<span class="comment">/**/</span>Disposable d)</span> </span>&#123;</div><div class="line">               Log.d(TAG, <span class="string">"onSubscribe: "</span>);</div><div class="line">           &#125;</div><div class="line">           <span class="meta">@Override</span></div><div class="line">           <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onNext</span><span class="params">(String string)</span> </span>&#123;</div><div class="line">               Log.d(TAG, <span class="string">"zip操作结果: "</span> + string);</div><div class="line">           &#125;</div><div class="line">           <span class="meta">@Override</span></div><div class="line">           <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onError</span><span class="params">(Throwable e)</span> </span>&#123;</div><div class="line">               Log.d(TAG, <span class="string">"onError: "</span>);</div><div class="line">           &#125;</div><div class="line">           <span class="meta">@Override</span></div><div class="line">           <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onComplete</span><span class="params">()</span> </span>&#123;</div><div class="line">               Log.d(TAG, <span class="string">"onComplete: "</span>);</div><div class="line">           &#125;</div><div class="line">       &#125;);</div></pre></td></tr></table></figure>
</li>
<li><p>需要注意的几点内容：</p>
<ol>
<li><p>当两个上游处于同一线程时，他们的发射顺序将会先发送完第一个上游的事件，在发送第二个，而在发送第二个的过程中进行zip，因此，这种情况下的每一个上游的所有事件都会发射。</p>
</li>
<li><p>当两个上游不在同一线程时，他们才是异步的。</p>
</li>
</ol>
</li>
</ol>
  
	</div>
		<footer class="article-footer clearfix">
<div class="article-catetags">

<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/categories/RXJava系列/">RXJava系列</a>
</div>


  <div class="article-tags">
  
  <span></span> <a href="/tags/RxJava/">RxJava</a><a href="/tags/RxJava基础/">RxJava基础</a><a href="/tags/RxJava操作符/">RxJava操作符</a>
  </div>

</div>



	<div class="article-share" id="share">
	
	  <div data-url="http://www.ryanhuen.tech/2017/02/17/rx-base-operator/" data-title="RxJava基础篇(三):常用操作符 | RyanHuen" data-tsina="null" class="share clearfix">
	  </div>
	
	</div>


</footer>

   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/2017/02/21/rx-backpressure/" title="RxJava基础篇(四):BackPressure简介">
  <strong>上一篇：</strong><br/>
  <span>
  RxJava基础篇(四):BackPressure简介</span>
</a>
</div>


<div class="next">
<a href="/2017/02/16/rx-thread-control/"  title="RxJava基础篇(二):线程控制">
 <strong>下一篇：</strong><br/> 
 <span>RxJava基础篇(二):线程控制
</span>
</a>
</div>

</nav>

	
<section id="comments" class="comment">
	<div class="ds-thread" data-thread-key="2017/02/17/rx-base-operator/" data-title="RxJava基础篇(三):常用操作符" data-url="http://www.ryanhuen.tech/2017/02/17/rx-base-operator/"></div>
</section>


</div>  
      <div class="openaside"><a class="navbutton" href="#" title="显示侧边栏"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">文章目录</strong>
 
 <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#变换："><span class="toc-number">1.</span> <span class="toc-text">变换：</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#变换操作符："><span class="toc-number">2.</span> <span class="toc-text">变换操作符：</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Map："><span class="toc-number">2.1.</span> <span class="toc-text">Map：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#FlatMap："><span class="toc-number">2.2.</span> <span class="toc-text">FlatMap：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#ConcatMap："><span class="toc-number">2.3.</span> <span class="toc-text">ConcatMap：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Zip："><span class="toc-number">2.4.</span> <span class="toc-text">Zip：</span></a></li></ol></li></ol>
 
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="隐藏侧边栏"></a></div>
<aside class="clearfix">

  
<div class="categorieslist">
	<p class="asidetitle">分类</p>
		<ul>
		
		  
			<li><a href="/categories/Emacs系列/" title="Emacs系列">Emacs系列<sup>3</sup></a></li>
		  
		
		  
			<li><a href="/categories/Framework系列/" title="Framework系列">Framework系列<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/categories/RXJava系列/" title="RXJava系列">RXJava系列<sup>7</sup></a></li>
		  
		
		  
			<li><a href="/categories/git入门系列/" title="git入门系列">git入门系列<sup>5</sup></a></li>
		  
		
		  
			<li><a href="/categories/Emacs系列/org-mode/" title="org-mode">org-mode<sup>1</sup></a></li>
		  
		
		</ul>
</div>


  
<div class="tagslist">
	<p class="asidetitle">标签</p>
		<ul class="clearfix">
		
			
				<li><a href="/tags/RxJava/" title="RxJava">RxJava<sup>7</sup></a></li>
			
		
			
				<li><a href="/tags/git/" title="git">git<sup>5</sup></a></li>
			
		
			
				<li><a href="/tags/RxJava基础/" title="RxJava基础">RxJava基础<sup>4</sup></a></li>
			
		
			
				<li><a href="/tags/Spacemacs/" title="Spacemacs">Spacemacs<sup>3</sup></a></li>
			
		
			
				<li><a href="/tags/Emacs/" title="Emacs">Emacs<sup>3</sup></a></li>
			
		
			
				<li><a href="/tags/观察者模式/" title="观察者模式">观察者模式<sup>3</sup></a></li>
			
		
			
				<li><a href="/tags/BackPressure/" title="BackPressure">BackPressure<sup>1</sup></a></li>
			
		
			
				<li><a href="/tags/org/" title="org">org<sup>1</sup></a></li>
			
		
			
				<li><a href="/tags/Java回调/" title="Java回调">Java回调<sup>1</sup></a></li>
			
		
			
				<li><a href="/tags/Debug/" title="Debug">Debug<sup>1</sup></a></li>
			
		
			
				<li><a href="/tags/设计模式/" title="设计模式">设计模式<sup>1</sup></a></li>
			
		
			
				<li><a href="/tags/RxJava操作符/" title="RxJava操作符">RxJava操作符<sup>1</sup></a></li>
			
		
			
				<li><a href="/tags/响应式编程/" title="响应式编程">响应式编程<sup>1</sup></a></li>
			
		
			
				<li><a href="/tags/Android-Studio/" title="Android Studio">Android Studio<sup>1</sup></a></li>
			
		
			
				<li><a href="/tags/Framework/" title="Framework">Framework<sup>1</sup></a></li>
			
		
			
				<li><a href="/tags/org-mode/" title="org-mode">org-mode<sup>1</sup></a></li>
			
		
		</ul>
</div>


  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS 订阅</a>
</div>

</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<div class="line">
		<span></span>
		<div class="author"></div>
	</div>
	
	
	<section class="info">
		<p> Hi there,I&#39;m RyanHuen <br/>
			A simple programmer</p>
	</section>
	 
	<div class="social-font" class="clearfix">
		
		<a href="http://weibo.com/ryanxuan" target="_blank" class="icon-weibo" title="微博"></a>
		
		
		<a href="https://github.com/RyanHuen" target="_blank" class="icon-github" title="github"></a>
		
		
		
		
		
		
		
		<a href="http://www.zhihu.com/people/ryan-hyn" target="_blank" class="icon-zhihu" title="知乎"></a>
		
		
		
		<a href="mailto:ryanhuenwork@163.com" target="_blank" class="icon-email" title="Email Me"></a>
		
	</div>
			
		

		<p class="copyright">
		Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/wuchong/jacman" target="_blank" title="Jacman">Jacman</a> © 2017 
		
		<a href="/about" target="_blank" title="RyanHuen">RyanHuen</a>
		
		
		</p>
</div>
</footer>
    <script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/jquery.imagesloaded.min.js"></script>
<script src="/js/gallery.js"></script>
<script src="/js/jquery.qrcode-0.12.0.min.js"></script>

<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
  
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else{
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  c.click(function(){
    ta.css('display', 'block').addClass('fadeIn');
  });
  o.click(function(){
    ta.css('display', 'none');
  });
  $(window).scroll(function(){
    ta.css("top",Math.max(140,320-$(this).scrollTop()));
  });
});
</script>


<script type="text/javascript">
$(document).ready(function(){ 
  var $this = $('.share'),
      url = $this.attr('data-url'),
      encodedUrl = encodeURIComponent(url),
      title = $this.attr('data-title'),
      tsina = $this.attr('data-tsina'),
      description = $this.attr('description');
  var html = [
  '<div class="hoverqrcode clearfix"></div>',
  '<a class="overlay" id="qrcode"></a>',
  '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
  '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
  '<a href="#qrcode" class="article-share-qrcode" title="微信"></a>',
  '<a href="http://widget.renren.com/dialog/share?resourceUrl=' + encodedUrl + '&srcUrl=' + encodedUrl + '&title=' + title +'" class="article-share-renren" target="_blank" title="人人"></a>',
  '<a href="http://service.weibo.com/share/share.php?title='+title+'&url='+encodedUrl +'&ralateUid='+ tsina +'&searchPic=true&style=number' +'" class="article-share-weibo" target="_blank" title="微博"></a>',
  '<span title="Share to"></span>'
  ].join('');
  $this.append(html);

  $('.hoverqrcode').hide();

  var myWidth = 0;
  function updatehoverqrcode(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
    var qrsize = myWidth > 1024 ? 200:100;
    var options = {render: 'image', size: qrsize, fill: '#2ca6cb', text: url, radius: 0.5, quiet: 1};
    var p = $('.article-share-qrcode').position();
    $('.hoverqrcode').empty().css('width', qrsize).css('height', qrsize)
                          .css('left', p.left-qrsize/2+20).css('top', p.top-qrsize-10)
                          .qrcode(options);
  };
  $(window).resize(function(){
    $('.hoverqrcode').hide();
  });
  $('.article-share-qrcode').click(function(){
    updatehoverqrcode();
    $('.hoverqrcode').toggle();
  });
  $('.article-share-qrcode').hover(function(){}, function(){
      $('.hoverqrcode').hide();
  });
});   
</script>



<script type="text/javascript">
  var duoshuoQuery = {short_name:"ryanhuen"};
  (function() {
    var ds = document.createElement('script');
    ds.type = 'text/javascript';ds.async = true;
    ds.src = '//static.duoshuo.com/embed.js';
    ds.charset = 'UTF-8';
    (document.getElementsByTagName('head')[0] 
    || document.getElementsByTagName('body')[0]).appendChild(ds);
  })();
</script> 







<link rel="stylesheet" href="/fancybox/jquery.fancybox.css" media="screen" type="text/css">
<script src="/fancybox/jquery.fancybox.pack.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.article-content').each(function(i){
    $(this).find('img').each(function(){
      if ($(this).parent().hasClass('fancybox')) return;
      var alt = this.alt;
      if (alt) $(this).after('<span class="caption">' + alt + '</span>');
      $(this).wrap('<a href="' + this.src + '" title="' + alt + '" class="fancybox"></a>');
    });
    $(this).find('.fancybox').each(function(){
      $(this).attr('rel', 'article' + i);
    });
  });
  if($.fancybox){
    $('.fancybox').fancybox();
  }
}); 
</script>



<!-- Analytics Begin -->



<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?af124d045b4aed59105f75db52359992";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>



<!-- Analytics End -->

<!-- Totop Begin -->

	<div id="totop">
	<a title="返回顶部"><img src="/img/scrollup.png"/></a>
	</div>
	<script src="/js/totop.js"></script>

<!-- Totop End -->

<!-- MathJax Begin -->
<!-- mathjax config similar to math.stackexchange -->


<!-- MathJax End -->

<!-- Tiny_search Begin -->

<!-- Tiny_search End -->

  </body>
</html>
